var app = getApp(), u = app.require("utils/util.js"), a = app.require("utils/api.js"), o = app.require("utils/onfire.js"), h = app.require("utils/http.js"), n = app.require("utils/enums");

var editAddress = '';
o.on('editAddress', function (data) {
  editAddress =  data;
});

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    provinceArr: [],
    proviteIds: [],
    cityIds: [],
    cityArr: [],
    areaIds: [],
    areaArr: [],
    pID: null,
    cID: null,
    aID: null,
    address_id:'',
    editAddress:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.address_id){
      this.data.address_id = options.address_id;      
      this.data.editAddress = editAddress;
      this.setData({
        address_id: this.data.address_id,
        editAddress: this.data.editAddress
      });
    }    
    this.data.userInfo = wx.getStorageSync('userInfo');
    this.getProvince();
  },
  onShow:function(){

  },
  PickerChangeProvince: function (e) {
    // console.log(e);
    var t = this;
    var pid = e.detail.value;
    t.getCityAreaByProvince(t.data.proviteIds[pid]);
    t.setData({
      address_id:'',
      pID: pid,
      cID: '',
      aID: '',
    })
  },
  PickerChangeCity: function (e) {
    var  t = this;
    var cid = e.detail.value;
    var pid = t.data.pID;
    var checkProvinceIds = t.data.proviteIds[pid];
    var checkCityIds = t.data.cityIds[cid];
    var area = [];
    var areaids = [];
    var data = {
      province: checkProvinceIds, 
      city: checkCityIds
    };
    h.postReq(a.getCityAreaList, data, function (res) {      
      if (res.code == 0) {
        var newkey = Object.keys(res.data.info);
        var newval = Object.values(res.data.info);
        for (var i = 0; i < newkey.length; i++) {
          area.push(newval[i]['name']);
          areaids.push(newval[i]['code']);
        }
        t.setData({
          address_id: '', 
          cID: cid, 
          areaArr: area, 
          areaIds: areaids 
        });
      }else{
        u.toast(res.message);
      }
    });
  },
  PickerChangeArea: function (e) {
    var t = this;
    t.setData({ address_id:'', aID: e.detail.value });
  },
  // 获取省份信息
  getProvince: function () {
    var t = this;
    h.getReq(a.getProvinceUrl, function (res) {
      if (res.code == 0) {
        var newkey = Object.keys(res.data);
        var newval = Object.values(res.data);
        t.setData({
           provinceArr: newval, 
           proviteIds: newkey 
        });
      }
    });
  },
  // 根据省份获取城市地区信息
  getCityAreaByProvince: function (provinceIds) {
    var t = this;
    var city = [];
    var cityids = [];
    var data = { 
      province: provinceIds
    }
    h.postReq(a.getCityAreaList, data, function (res) {
      if (res.code == 0) {
        var newkey = Object.keys(res.data);
        var newval = Object.values(res.data);
        for (var i = 0; i < newkey.length; i++) {
          city.push(newval[i]['name']);
          cityids.push(newval[i]['code']);
        }
        t.setData({ 
          cityArr: city, 
          cityIds: cityids 
        });
      }
    });    
  },
  onSubmit: function (e) {
    // console.log(e);return;
    var t = this;
    var data = e.detail.value;
    var addressId = e.detail.target.dataset.addressid ? e.detail.target.dataset.addressid : '';
    if (!data.name) {
      u.toast('姓名不能为空');
      return false;
    }
    if (!data.tel) {
      u.toast('手机号不能为空');
      return false;
    }
    if (!data.province) {
      u.toast('请选择省份信息');
      return false;
    }
    if (!data.city) {
      u.toast('请选择城市信息');
      return false;
    }
    if (!data.area) {
      u.toast('请选择地区信息');
      return false;
    }
    if (!data.address) {
      u.toast('请填写详情地址信息');
      return false;
    }
    if (!data.zipcode) {
      u.toast('请填写邮政编码');
      return false;
    }
    if (!(/^[0-9]*$/.test(data.zipcode))){
      u.toast('邮政编码必须是数字');
      return false;
    }
    var data = {
      uid: t.data.userInfo.uid, name: data.name, tel: data.tel, province: data.province, city: data.city, area: data.area, address: data.address, zipcode: data.zipcode, address_id: addressId
    }
    h.postReq(a.addressUpdateUrl, data, function (res) {
      if (res.code == 0) {
        u.success('设置成功',function(){
          wx.navigateBack();
        });        
      } 
    });
  },

})